package com.feeyo.monitor.service.imp;

import com.feeyo.monitor.domain.VehicleSim;
import com.feeyo.monitor.enumeration.SimStatus;
import com.feeyo.monitor.mapper.VehicleMapper;
import com.feeyo.monitor.mapper.VehicleSimMapper;
import com.feeyo.monitor.service.VehicleSimService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.postgresql.util.PSQLException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author <a href="mailto:bliulei@variflight.com">liulei</a>
 * @Description Created by liulei on 2017/5/15 下午6:02.
 */
@Service
public class VehicleSimServiceImp implements VehicleSimService {
    @Autowired
    VehicleSimMapper vehicleSimMapper;
    public static final long dayMililons= 24*60*60*1000;
    @Override
    public PageInfo selectByPage(Map param, int pageNumber, int pageSize) {
        PageHelper.startPage(pageNumber, pageSize);
        List<VehicleSim> result = vehicleSimMapper.selectByPage(param);
        Long now = new Date().getTime();                     //2017-06-13 17:52:00 now
        for(VehicleSim sim : result){
            Long timestamp = sim.getInvalidtime().getTime() + dayMililons; //2017-06-13 00:00:00 invalidTime
            if (now > timestamp) //已过期
                sim.setStatus((short) SimStatus.arrearage.getKey());
            else    //未过期
                sim.setStatus((short)SimStatus.using.getKey());
        }

        PageInfo pi = new PageInfo(result);
        return pi;
    }

    @Override
    public VehicleSim selectByPrimaryKey(Integer id) throws PSQLException {
        return vehicleSimMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateDeleteStatus(Integer simId) {

        return vehicleSimMapper.updateDeleteStatus(simId);
    }

    @Override
    public int insert(VehicleSim record) {
        return vehicleSimMapper.insert(record);
    }

    @Override
    public int updateByPrimaryKeySelective(VehicleSim record) {
        return vehicleSimMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByBatch(Map param) {
        return vehicleSimMapper.updateByBatch(param);
    }
}
